<template>
  <!-- 供应商发货包装信息表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="30%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="生产批次号" prop="supplyBatch">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.supplyBatch"
          placeholder="请输入生产批次"
          style="width: 300px"
          clearable
        />

        <span v-else>{{ formData.supplyBatch }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import { ref, watch, reactive, onBeforeUnmount } from "vue";
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "SRMSRMORDERUNITADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  organizationId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  organizationCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  organizationName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  plantId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  plantCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  plantName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  partnerId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  partnerCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  parentUnitId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  topUnitId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  deliveryId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialNumber: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  supplyBatch: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  arriveDate: [],
  orderScheduleId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  supplyMaterialType: [],
  unitBarcode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialQuantity: [],
  unitCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  ownAssets: [],
  unitLevel: [],
  unitShowFlag: [],
  version: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  deleted: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  organizationId: "",
  organizationCode: "",
  organizationName: "",
  plantId: "",
  plantCode: "",
  plantName: "",
  partnerId: "",
  partnerCode: "",
  parentUnitId: "",
  topUnitId: "",
  deliveryId: "",
  materialId: "",
  materialNumber: "",
  materialName: "",
  supplyBatch: "",
  arriveDate: "",
  orderScheduleId: "",
  supplyMaterialType: "",
  unitBarcode: "",
  materialQuantity: "",
  unitCode: "",
  ownAssets: "",
  unitLevel: "",
  unitShowFlag: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
  ids: [],
});

watch(
  () => props.data,
  (newVal: any) => {
    if (true) {
      const {
        id,
        organizationId,
        organizationCode,
        organizationName,
        plantId,
        plantCode,
        plantName,
        partnerId,
        partnerCode,
        parentUnitId,
        topUnitId,
        deliveryId,
        materialId,
        materialNumber,
        materialName,
        supplyBatch,
        arriveDate,
        orderScheduleId,
        supplyMaterialType,
        unitBarcode,
        materialQuantity,
        unitCode,
        ownAssets,
        unitLevel,
        unitShowFlag,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
        ids,
      } = newVal;
      Object.assign(formData, {
        id,
        organizationId,
        organizationCode,
        organizationName,
        plantId,
        plantCode,
        plantName,
        partnerId,
        partnerCode,
        parentUnitId,
        topUnitId,
        deliveryId,
        materialId,
        materialNumber,
        materialName,
        supplyBatch,
        arriveDate,
        orderScheduleId,
        supplyMaterialType,
        unitBarcode,
        materialQuantity,
        unitCode,
        ownAssets,
        unitLevel,
        unitShowFlag,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
        ids,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.organizationName = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.plantName = "";
  formData.partnerId = "";
  formData.partnerCode = "";
  formData.parentUnitId = "";
  formData.topUnitId = "";
  formData.deliveryId = "";
  formData.materialId = "";
  formData.materialNumber = "";
  formData.materialName = "";
  formData.supplyBatch = "";
  formData.arriveDate = "";
  formData.orderScheduleId = "";
  formData.supplyMaterialType = "";
  formData.unitBarcode = "";
  formData.materialQuantity = "";
  formData.unitCode = "";
  formData.ownAssets = "";
  formData.unitLevel = "";
  formData.unitShowFlag = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
  formData.ids = [];
}

const loading = ref(false);

/** 供应商发货包装信息保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;

      await request({
        url: "/srm/core/srmorderunit/batch",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(`编辑成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
